Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/add solution class #11

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

MWL91
Copy link

@MWL91 MWL91 commented May 9, 2022

#9 I've created (in my opinion) clearer way to product linear program.
Now it all may be written as a one class, and from that class you may take answers.

Using LinearProgram class

Using \Simplex\Formulation\LinearProgram you can formulate linear program solution.

For example let's assume, that we have given linear problem:

z = 2x1 + x2

with constraints:

x1 + x2 >= 2

x1 - x2 = 0

x1 - x2 >= -4

that we can describe in following way:

use \Simplex\Formulation\LinearProgram;
use \Simplex\Formulation\Formula;
use \Simplex\Formulation\GreaterOrEqual;
use \Simplex\Formulation\Equal;

$program = new LinearProgram(
    new Formula(2, 1),
    [
        new GreaterOrEqual(new Formula(1, 1), 2), 
        new Equal(new Formula(1, -1), 0), 
        new GreaterOrEqual(new Formula(1, -2), -4),
    ]
);

Now we can take maximum solution with only:

$program->getMax(); // return 12

And optimized parameters:

$solution = $program->getSolutionFormula();
// returns new Formula(4, 4, 6, 0);

$solution->getParam('x1'); // 4
$solution->getParam(1); // the same as upper 4

@uestla uestla force-pushed the master branch 26 times, most recently from e7cd3ea to 271bd54 Compare February 12, 2023 13:28
@uestla
Copy link
Owner

uestla commented Mar 12, 2023

@MWL91 Hello and sorry for reacting this late after your PR.

The library has gone through bigger refactoring lately and the core was rewritten to use bcmath for better precision. The API however stood the same, and some more helpful methods were added (including getSolution() and getSolutionValue() on the Solver class - which behaves practically the same as your getMax() method).

Anyway, I have to say I really like your API since it removes the variable naming ("x1, x2, ...") and adds more OOP-way of doing things. Vector of coefficients where index is the input variable is really clever. Also having multiple constraint classes for each type (GOE, LOE, EQUAL) is also much better approach.

I'll think about changing the API and maybe release new major version after that.

What I would like to keep however is the support for older PHP versions (even if they're already in EOL state).

@uestla uestla force-pushed the master branch 2 times, most recently from c5f9a77 to 0772e67 Compare March 19, 2023 09:20
@uestla uestla force-pushed the master branch 2 times, most recently from ec49543 to 5fd4e7d Compare March 25, 2023 10:05
@uestla uestla force-pushed the master branch 11 times, most recently from 36bef98 to d6530df Compare April 7, 2023 14:24
@uestla uestla force-pushed the master branch 6 times, most recently from c2715c6 to c2ec946 Compare April 18, 2023 07:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants